<?php
  class recent_history {
    var $viewed_items, $searches, $viewed_cats, $on, $pmax, $smax;

    function recent_history ($maxi = 5, $maxc = 12, $maxs = 12) {
      $this->viewed_items = array();
      $this->viewed_cats = array();
      $this->searches = array();
      $this->pmax = $maxi;
      $this->cmax = $maxc;
      $this->smax = $maxs;
      $this->on = true;
    }

    function reset() {
      $this->viewed_items = array();
      $this->searches = array();
      $this->viewed_cats = array();
    }

    function cp() {
      $this->viewed_items = array();
    }

    function cs() {
      $this->searches = array();
    }

    function cc() {
      $this->viewed_cats = array();
    }

// PRODUCTS

    function add_viewed($products_id) {

      if (!in_array($products_id, $this->viewed_items)) {
        $tmp1 = array();

        $current_count = sizeof($this->viewed_items);
        if ($current_count < $this->pmax) {
          $limit = $current_count - 1;
        } else {
          $limit = $this->pmax - 2;
        }
        $tmp1[] = $products_id;

        for ($i=0; $i <= $limit; $i++) {
          $tmp1[] = $this->viewed_items[$i];
        }
        $this->viewed_items = array();
        $this->viewed_items = $tmp1;
      }
    }

    function count_viewed() { 
      return sizeof($this->viewed_items);
    }

    function get_viewed_items() {
      return $this->viewed_items;
    }

// SEARCHES

    function add_search($keywords) {
      if (!in_array($keywords, $this->searches)) {
        $tmp = array();
        $current_count = sizeof($this->searches);
        if ($current_count < $this->smax) {
          $limit = $current_count - 1;
        } else {
          $limit = $this->smax - 2;
        }
        $tmp[] = $keywords;
        for ($i=0; $i <= $limit; $i++) {
          $tmp[] = $this->searches[$i];
        }
        $this->searches = array();
        $this->searches = $tmp;
      }
    }

    function count_searches() { 
      return sizeof($this->searches);
    }

    function get_searches() {
      return $this->searches;
    }

// categories

    function add_cat($cPath) {
      if (!in_array($cPath, $this->viewed_cats)) {
        $tmp1 = array();

        $current_count = sizeof($this->viewed_cats);
        if ($current_count < $this->cmax) {
          $limit = $current_count - 1;
        } else {
          $limit = $this->cmax - 2;
        }
        $tmp1[] = $cPath;

        for ($i=0; $i <= $limit; $i++) {
          $tmp1[] = $this->viewed_cats[$i];
        }
        $this->viewed_cats = array();
        $this->viewed_cats = $tmp1;
      }
    }

    function count_viewed_cats() { 
      return sizeof($this->viewed_cats);
    }

    function get_viewed_cats() {
      return $this->viewed_cats;
    }
  }
?>